﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using TV_Agenda_V2.Objects;
using System.Runtime.Remoting.Messaging;
using System.Windows.Forms;

namespace TV_Agenda_V2.Utilities
{
    abstract class IShowDataSource
    {
        private Form parentForm;

        public IShowDataSource(Form parent)
        {
            parentForm = parent;
        }


        #region ProgressDialog
        
        internal ProgressDialog progressDialog;

        /// <summary>
        /// Initializes a new progress dialog.
        /// </summary>
        public void InitializeNewProgressDialog()
        {
            progressDialog = new ProgressDialog();
            progressDialog.Show(parentForm);
        }


        /// <summary>
        /// Sets the progress maximum.
        /// </summary>
        /// <param name="max">The max.</param>
        public void SetProgressMaximum(Int32 max)
        {
            if (progressDialog == null)
                InitializeNewProgressDialog();
            progressDialog.SetMaximum(max);
        }

        /// <summary>
        /// Sets the progress.
        /// </summary>
        /// <param name="progress">The progress.</param>
        public void SetProgress(Int32 progress)
        {
            if (progressDialog == null)
                InitializeNewProgressDialog();
            progressDialog.SetProgress(progress);
        }

        public void CloseProgressDialog()
        {
            if (progressDialog != null)
                progressDialog.CloseDialog();
        }
        #endregion

        #region Search show
        /// <summary>
        /// Search delegate for call back.
        /// </summary>
        /// <param name="showName">Name of the show.</param>
        /// <returns>List of show results.</returns>
        public delegate List<Show> SearchShowDelegate(string showName);
        
        /// <summary>
        /// Searches for a show.
        /// </summary>
        /// <param name="showName">Name of the show.</param>
        /// <returns>A list of search results.</returns>
        public abstract List<Show> SearchShow(String showName);

        #region Async methods
        /// <summary>
        /// Begins to search a show.
        /// </summary>
        /// <param name="showName">Name of the show.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>The IAsyncResult.</returns>
        public IAsyncResult BeginSearchShow(String showName, AsyncCallback callback)
        {
            SearchShowDelegate del = new SearchShowDelegate(SearchShow);
            return del.BeginInvoke(showName, callback, null);
        }

        /// <summary>
        /// Ends the search for a show.
        /// </summary>
        /// <param name="result">The result.</param>
        /// <returns>An XML Document with all the shows found by the search.</returns>
        public List<Show> EndSearchShow(IAsyncResult result)
        {
            AsyncResult res = (AsyncResult)result;
            SearchShowDelegate del = (SearchShowDelegate)res.AsyncDelegate;
            return del.EndInvoke(result);
        }
        #endregion

        #endregion

        #region Update show info
        /// <summary>
        /// Show update delegate for call back.
        /// </summary>
        /// <param name="show">The show to update.</param>
        public delegate void UpdateShowInfoDelegate(Show show);

        /// <summary>
        /// Updates the show info.
        /// </summary>
        /// <param name="show">The show.</param>
        public abstract void UpdateShowInfo(Show show);

        #region Async methods
        /// <summary>
        /// Begins to update a show.
        /// </summary>
        /// <param name="showName">Name of the show.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>The IAsyncResult.</returns>
        public IAsyncResult BeginUpdateShow(Show show, AsyncCallback callback)
        {
            UpdateShowInfoDelegate del = new UpdateShowInfoDelegate(UpdateShowInfo);
            return del.BeginInvoke(show, callback, null);
        }

        /// <summary>
        /// Ends the update for a show.
        /// </summary>
        /// <param name="result">The result.</param>
        public void EndUpdateShow(IAsyncResult result)
        {
            AsyncResult res = (AsyncResult)result;
            UpdateShowInfoDelegate del = (UpdateShowInfoDelegate)res.AsyncDelegate;
            del.EndInvoke(result);
        }
        #endregion

        #endregion

        #region Update episode list

        public delegate void UpdateEpisodesDelegate(Show show);

        public abstract void UpdateEpisodes(Show show);

        #region Async methods
        /// <summary>
        /// Begins to update the episodelist of a show.
        /// </summary>
        /// <param name="showName">The show.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>The IAsyncResult.</returns>
        public IAsyncResult BeginUpdateEpisodes(Show show, AsyncCallback callback)
        {
            UpdateEpisodesDelegate del = new UpdateEpisodesDelegate(UpdateEpisodes);
            return del.BeginInvoke(show, callback, null);
        }

        /// <summary>
        /// Ends the episode update.
        /// </summary>
        /// <param name="result">The result.</param>
        public void EndUpdateEpisodes(IAsyncResult result)
        {
            AsyncResult res = (AsyncResult)result;
            UpdateEpisodesDelegate del = (UpdateEpisodesDelegate)res.AsyncDelegate;
            del.EndInvoke(result);
        }
        #endregion

        #endregion

        #region Update shows
        
        public delegate void UpdateShowsDelegate(ShowList showList);

        public abstract void UpdateShows(ShowList showList);

        #region Async methods
        public IAsyncResult BeginUpdateShows(ShowList showList, AsyncCallback callback)
        {
            InitializeNewProgressDialog();
            UpdateShowsDelegate del = new UpdateShowsDelegate(UpdateShows);
            return del.BeginInvoke(showList, callback, null);
        }

        public void EndUpdateShows(IAsyncResult result)
        {
            AsyncResult res = (AsyncResult)result;
            UpdateShowInfoDelegate del = (UpdateShowInfoDelegate)res.AsyncDelegate;
            del.EndInvoke(result);
        }
        #endregion
        #endregion
    }
}
